package com.sp.scmdb.action;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.sp.scmdb.bean.PathBean;
import com.sp.scmdb.bean.UserBean;
import com.sp.scmdb.common.CommonParamter;
import com.sp.scmdb.common.action.BaseAction;
import com.sp.scmdb.entity.LabelReadingMachine;
import com.sp.scmdb.entity.Map;
import com.sp.scmdb.service.MapService;
import com.sp.scmdb.service.PathService;
import com.sp.scmdb.service.ReaderService;

@Controller("pathAction")
@Scope("prototype")
public class PathAction extends BaseAction<PathBean> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static Log log = LogFactory.getLog(PathAction.class);
	private PathService pathService;
	private ReaderService readerService;
	private MapService mapService;

	@Autowired
	public void setMapService(MapService mapService) {
		this.mapService = mapService;
	}

	@Autowired
	public void setReaderService(ReaderService readerService) {
		this.readerService = readerService;
	}

	@Autowired
	public void setPathService(PathService pathService) {
		this.pathService = pathService;
	}

	@SuppressWarnings("unchecked")
	public String findAll() {
		try {
			PathBean pathBean = null;
			if (StringUtils.isNotEmpty(pageName)) {
				pathBean = (PathBean) session.get("pathBean");
			}
			setEntity(pathService.queryPathAll(entity, pathBean));
			session.put("pathBean", entity);
			this.button("Super.Popup(5,'添加','/SCMDB/pathAction!newPath.action','',1000,600)",
					"Super.editData('/SCMDB/pathAction!findPath.action?id=',1000,600)",
					"Super.delData('/SCMDB/pathAction!delPath.action?pId=')");
			return SUCCESS;
		} catch (Exception e) {
			log.error("get path failed! reason:" + e.getMessage());
			return "fail";
		}
	}

	public String newPath() {
		try {
			List<LabelReadingMachine> listRead = readerService.queryAll();
			request.setAttribute("listRead", listRead);
			List<Map> listMap = mapService.queryIsUsed();
			request.setAttribute("listMap", listMap);
			return "newSucc";
		} catch (Exception e) {
			log.error("new init path failed! reason:" + e.getMessage());
			return "fail";
		}
	}

	public String findPath() {
		try {
			String id = request.getParameter("id");
			if (StringUtils.isNotEmpty(id)) {
				List<LabelReadingMachine> listRead = readerService.queryAll();
				request.setAttribute("listRead", listRead);
				List<Map> listMap = mapService.queryIsUsed();
				request.setAttribute("listMap", listMap);
				setEntity(pathService.queryPathByID(id));
				return "editSucc";
			} else {
				return "fail";
			}
		} catch (Exception e) {
			log.error("find init edit path failed! reason:" + e.getMessage());
			return "fail";
		}
	}

	public String addPath() {
		try {
			UserBean user = (UserBean) session.get("user");
			String path = entity.getPath();
			String result = "";
			if (StringUtils.isNotEmpty(path)) {
				result = pathService.addPath(entity, user);
			}
			messageSession(result, "路径");
			return "commitSucc";
		} catch (Exception e) {
			log.error("add path failed! reason:" + e.getMessage());
			return "fail";
		}
	}

	public String editPath() {
		try {
			UserBean user = (UserBean) session.get("user");
			String path = entity.getPath();
			String result = "";
			if (StringUtils.isNotEmpty(path)) {
				result = pathService.editPath(entity, user);
			}
			messageSession(result, "路径");
			return "commitSucc";
		} catch (Exception e) {
			log.error("update path failed! reason:" + e.getMessage());
			return "fail";
		}
	}

	public String delPath() {
		String result = null;
		try {
			UserBean user = (UserBean) session.get("user");
			String pId = request.getParameter("pId");
			if (StringUtils.isNotEmpty(pId)) {
				String[] arr = pId.split(",");
				result = pathService.delPath(arr, user);
			}
			messageSession(result, "路径");
			return "commitSucc";
		} catch (Exception e) {
			log.error("delete path failed! reason:" + e.getMessage());
			return "fail";
		}
	}
	@SuppressWarnings("unchecked")
	public String findUpload() {
		try {
			PathBean pathBean = null;
			if (StringUtils.isNotEmpty(pageName)) {
				pathBean = (PathBean) session.get("pathBean");
			}
			setEntity(pathService.queryPathAll(entity, pathBean));
			session.put("pathBean", entity);
			return "findUpload";
		} catch (Exception e) {
			log.error("get path failed! reason:" + e.getMessage());
			return "fail";
		}
	}
	public String upload() {
		try{
			String id = request.getParameter("id");
			String result = pathService.upload(id);
			if (result.equals("timeOut")) {
				messageSession(CommonParamter.CHECK_FLG, "连接超时");
			} else if (result.equals("success")) {
				messageSession(CommonParamter.CHECK_SUCCESS, "上传数据");
			} else if (result.equals(CommonParamter.CHECK_FAIL)) {
				messageSession(CommonParamter.CHECK_FAIL, "上传数据");
			}else{
				messageSession(CommonParamter.CHECK_FLG,result);
			}
		} catch (Exception e) {
			log.error("find leadership error!reason:" + e.getMessage());
			return "fail";
		}
		return "upload";
	}
	
}
